package composite

import "fmt"

type FileSystemNode interface {
	Display()
	setParent(node FileSystemNode)
}

type File struct {
	Name   string
	parent FileSystemNode
}

func (f *File) setParent(node FileSystemNode) {
	f.parent = node
}

func (f *File) Display() {
	fmt.Println(f.Name)
}

type Director struct {
	Name   string
	parent FileSystemNode
	nodes  map[string]FileSystemNode
}

func (d *Director) GetSubNode(name string) (FileSystemNode, bool) {
	node, ok := d.nodes[name]
	return node, ok
	//return d.nodes[name]
}

func (d *Director) setParent(node FileSystemNode) {
	d.parent = node
}

func NewDirector(name string) *Director {
	return &Director{
		Name:  name,
		nodes: make(map[string]FileSystemNode),
	}
}

func (d *Director) AddNode(name string, f FileSystemNode) {
	d.nodes[name] = f
	f.setParent(d)
}

func (d *Director) RemoveNode(name string) {
	delete(d.nodes, name)
}

func (d *Director) Display() {
	fmt.Println(d.Name)
	for _, node := range d.nodes {
		node.Display()
	}
}
